<template>
  <div>
    <div class="search">
      <input v-model="keyword"  class="search-input"  type="text" placeholder="输入城市名或拼音"/>
    </div>
    <div class="search-content" v-show="keyword" ref="search">
      <ul>
        <li class="search-item border-bottom"
            v-for="item of list"
        >
          {{item}}
        </li>
        <li class="search-item border-bottom" v-show="letter">
          暂无数据
        </li>
      </ul>

    </div>

  </div>

</template>

<script>
import Bscroll from 'better-scroll'
import list from "./List";
export default {
  name: "Search",
  data(){
    return {
      keyword: "",
      list:[],
      timer: null
    }
  },
  props:{
    cities:Object
  },
  computed:{
      letter() {
        return this.list.length == 0;
      }
  },
  mounted() {
    this.scroll = new Bscroll(this.$refs.search,{
      click: true
    })

  },
  watch:{
    keyword (){
       if (this.timer){
         clearTimeout(this.timer)
       }
       if (!this.keyword){
           this.list=[];
           return
       }
      this.timer = setTimeout(()=>{
        const result = [];
        for (let i in this.cities) {
          this.cities[i].forEach((item)=>{
            if (item.spell.indexOf(this.keyword)!=-1 || item.name.indexOf(this.keyword)!=-1){
              result.push(item.name);
            }
          });
        }
        this.list = result;
        return result;

      },100);
    }
  }


}
</script>

<style lang="stylus" scoped>
  .search
    height .72rem
    background #00bcd4
    padding 0 .1rem
    .search-input
      width 100%
      height .6rem
      text-align center
      border-radius .1rem
      color #666
      padding 0 .1rem
      box-sizing border-box
  .search-content
    z-index 1
    position absolute
    top 1.58rem
    left 0
    right 0
    bottom 0
    background: #eee
    overflow hidden
    .search-item
      line-height .62rem
      padding-left .2rem
      color #333
      background white



</style>
